{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python3\n",
    "import os\n",
    "from parse_pdb import pdbfile\n",
    "from function import build_rna_list\n",
    "from rcsb import get_info, get_pdb_file\n",
    "from itertools import combinations\n",
    "from calculate import calculate_internal, calculate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "pdb_idlist = open(\"pdb_idlist.txt\").read().split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_method(pdb_id):\n",
    "    method = get_info(pdb_id)['PDBdescription']['PDB']['@expMethod']\n",
    "    key = method.split()[0]\n",
    "    if key == 'SOLUTION':\n",
    "        return \"NMR\"\n",
    "    if key == 'ELECTRON':\n",
    "        return \"MICROSCOPY\"\n",
    "    if key == 'X-RAY':\n",
    "        return 'X-RAY'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parsing pdb_file/X-RAY/6DCL.pdb\n",
      "Chain list: C D\n",
      "Missing atoms [('D', 11)]\n",
      "Parsing pdb_file/NMR/2RRA.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/2OZB.pdb\n",
      "Chain list: C F\n",
      "Parsing pdb_file/NMR/2KXN.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/NMR/2LA5.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/NMR/2M8D.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/NMR/2MKN.pdb\n",
      "Chain list: B C\n",
      "Parsing pdb_file/NMR/2N3O.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2N82.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/NMR/2MXY.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2MZ1.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/1DRZ.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2KG1.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2KG0.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2KFY.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2LEB.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2LEC.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2L5D.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/2G4B.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/2ANR.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/2ANN.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/3L3C.pdb\n",
      "Chain list: E P F Q G R H S\n",
      "Missing atoms [('P', 91), ('R', 85), ('H', 6), ('S', 49), ('S', 17), ('P', 85), ('E', -1), ('Q', 85), ('P', 134), ('E', 6), ('Q', 49), ('P', 17)]\n",
      "Parsing pdb_file/X-RAY/3MUT.pdb\n",
      "Chain list: R\n",
      "Parsing pdb_file/X-RAY/3MUV.pdb\n",
      "Chain list: R\n",
      "Parsing pdb_file/X-RAY/3MUM.pdb\n",
      "Chain list: R\n",
      "Parsing pdb_file/X-RAY/3MUR.pdb\n",
      "Chain list: R\n",
      "Parsing pdb_file/X-RAY/3MXH.pdb\n",
      "Chain list: R\n",
      "Parsing pdb_file/X-RAY/3NCU.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3NNC.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 7)]\n",
      "Parsing pdb_file/X-RAY/3NNH.pdb\n",
      "Chain list: E F\n",
      "Missing atoms [('E', 12), ('F', 11), ('F', 1)]\n",
      "Parsing pdb_file/X-RAY/3NNA.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/3NMR.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 6)]\n",
      "Parsing pdb_file/X-RAY/3O6E.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/X-RAY/3O7V.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/X-RAY/3BSB.pdb\n",
      "Chain list: C\n",
      "Parsing pdb_file/X-RAY/3BSX.pdb\n",
      "Chain list: C D\n",
      "Missing atoms [('D', 2)]\n",
      "Parsing pdb_file/X-RAY/3EQT.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3G9C.pdb\n",
      "Chain list: E P F Q G R H S\n",
      "Missing atoms [('R', 73)]\n",
      "Parsing pdb_file/X-RAY/3G8T.pdb\n",
      "Chain list: E P F Q G R H S\n",
      "Missing atoms [('P', 17)]\n",
      "Parsing pdb_file/X-RAY/3G96.pdb\n",
      "Chain list: E P F Q G R H S\n",
      "Missing atoms [('P', 91), ('R', 85), ('H', 6), ('S', 49), ('S', 17), ('P', 85), ('E', -1), ('Q', 85), ('P', 134), ('E', 6), ('Q', 49), ('P', 17)]\n",
      "Parsing pdb_file/X-RAY/3G8S.pdb\n",
      "Chain list: E P F Q G R H S\n",
      "Missing atoms [('P', 91), ('R', 85), ('H', 6), ('S', 85), ('S', 49), ('S', 17), ('R', 49), ('P', 85), ('E', -1), ('Q', 85), ('P', 134), ('E', 6), ('Q', 49), ('P', 17)]\n",
      "Parsing pdb_file/X-RAY/3IRW.pdb\n",
      "Chain list: R\n",
      "Parsing pdb_file/X-RAY/3P49.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/X-RAY/3P6Y.pdb\n",
      "Chain list: Q R S T U V W\n",
      "Missing atoms [('T', 5), ('R', 4), ('Q', 2), ('U', 5), ('R', 5)]\n",
      "Parsing pdb_file/X-RAY/3Q0L.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3Q0M.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3Q0N.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3Q0O.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3Q0P.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/3Q0Q.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/3Q0R.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/3Q0S.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/3Q2T.pdb\n",
      "Chain list: E F\n",
      "Parsing pdb_file/X-RAY/4HOS.pdb\n",
      "Chain list: X\n",
      "Missing atoms [('X', 4)]\n",
      "Parsing pdb_file/X-RAY/4HOT.pdb\n",
      "Chain list: X\n",
      "Parsing pdb_file/X-RAY/4HOR.pdb\n",
      "Chain list: X\n",
      "Missing atoms [('X', 5)]\n",
      "Parsing pdb_file/X-RAY/4HT8.pdb\n",
      "Chain list: I K\n",
      "Missing atoms [('K', 2), ('K', 5)]\n",
      "Parsing pdb_file/X-RAY/4HT9.pdb\n",
      "Chain list: D E\n",
      "Parsing pdb_file/X-RAY/4ZLD.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4ED5.pdb\n",
      "Chain list: C D\n",
      "Missing atoms [('C', 2), ('D', 2), ('D', 11), ('C', 11)]\n",
      "Parsing pdb_file/X-RAY/4P3E.pdb\n",
      "Chain list: A\n",
      "Parsing pdb_file/X-RAY/4W90.pdb\n",
      "Chain list: C\n",
      "Missing atoms [('C', 15), ('C', 73), ('C', 57)]\n",
      "Parsing pdb_file/X-RAY/4W92.pdb\n",
      "Chain list: C\n",
      "Missing atoms [('C', 42), ('C', 57)]\n",
      "Parsing pdb_file/X-RAY/2YJY.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/4GL2.pdb\n",
      "Chain list: C E F D\n",
      "Parsing pdb_file/X-RAY/3R1L.pdb\n",
      "Chain list: B C E F\n",
      "Parsing pdb_file/X-RAY/3R1H.pdb\n",
      "Chain list: B C E F\n",
      "Parsing pdb_file/X-RAY/5EN1.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5EV1.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5EV3.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5EV2.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5EV4.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4YOE.pdb\n",
      "Chain list: E\n",
      "Parsing pdb_file/X-RAY/4Z31.pdb\n",
      "Chain list: C D E F\n",
      "Parsing pdb_file/X-RAY/5W5H.pdb\n",
      "Chain list: B D\n",
      "Parsing pdb_file/X-RAY/5W5I.pdb\n",
      "Chain list: B D\n",
      "Parsing pdb_file/X-RAY/5WWF.pdb\n",
      "Chain list: B D\n",
      "Missing atoms [('B', 10)]\n",
      "Parsing pdb_file/X-RAY/5WWE.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 10)]\n",
      "Parsing pdb_file/X-RAY/5WWG.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5WWX.pdb\n",
      "Chain list: C\n",
      "Parsing pdb_file/X-RAY/5WWW.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4PRF.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4PR6.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4QIK.pdb\n",
      "Chain list: C D\n",
      "Missing atoms [('C', 3)]\n",
      "Parsing pdb_file/X-RAY/4QIL.pdb\n",
      "Chain list: D C\n",
      "Parsing pdb_file/X-RAY/4R3I.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4QU6.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/4QU7.pdb\n",
      "Chain list: U V X\n",
      "Parsing pdb_file/X-RAY/4RCJ.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 5)]\n",
      "Parsing pdb_file/X-RAY/4C4W.pdb\n",
      "Chain list: D H\n",
      "Parsing pdb_file/NMR/5U9B.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5UDZ.pdb\n",
      "Chain list: V W\n",
      "Missing atoms [('W', 7)]\n",
      "Parsing pdb_file/X-RAY/5H3U.pdb\n",
      "Chain list: C\n",
      "Parsing pdb_file/X-RAY/6A6J.pdb\n",
      "Chain list: B D\n",
      "Parsing pdb_file/X-RAY/6A6L.pdb\n",
      "Chain list: D\n",
      "Parsing pdb_file/X-RAY/6JVX.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 0)]\n",
      "Parsing pdb_file/X-RAY/6L5N.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/6KTC.pdb\n",
      "Chain list: V\n",
      "Parsing pdb_file/X-RAY/6KUG.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5GXI.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 119), ('B', 123)]\n",
      "Parsing pdb_file/X-RAY/5GXH.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 2), ('B', 6)]\n",
      "Parsing pdb_file/X-RAY/5HO4.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 10), ('B', 6)]\n",
      "Parsing pdb_file/X-RAY/1EC6.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/NMR/2ADB.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/1M8Y.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/1M8X.pdb\n",
      "Chain list: C D\n",
      "Parsing pdb_file/X-RAY/1M8W.pdb\n",
      "Parsing pdb_file/X-RAY/1A9N.pdb\n",
      "Chain list: Q R\n",
      "Parsing pdb_file/NMR/1AUD.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/MICROSCOPY/6URO.pdb\n",
      "Chain list: D\n",
      "Parsing pdb_file/MICROSCOPY/6V5B.pdb\n",
      "Chain list: D\n",
      "Parsing pdb_file/MICROSCOPY/6V5C.pdb\n",
      "Chain list: D\n",
      "Parsing pdb_file/MICROSCOPY/6V4X.pdb\n",
      "Chain list: Z Y\n",
      "Parsing pdb_file/MICROSCOPY/6DNH.pdb\n",
      "Chain list: E\n",
      "Parsing pdb_file/MICROSCOPY/6LXD.pdb\n",
      "Chain list: D\n",
      "Parsing pdb_file/X-RAY/6UV2.pdb\n",
      "Chain list: C\n",
      "Missing atoms [('C', 2), ('C', 9)]\n",
      "Parsing pdb_file/X-RAY/6UV1.pdb\n",
      "Chain list: C D\n",
      "Missing atoms [('C', 2), ('D', 2)]\n",
      "Parsing pdb_file/X-RAY/6UV4.pdb\n",
      "Chain list: C\n",
      "Parsing pdb_file/X-RAY/6UV3.pdb\n",
      "Chain list: B\n",
      "Missing atoms [('B', 9)]\n",
      "Parsing pdb_file/X-RAY/6NIT.pdb\n",
      "Chain list: C D E F\n",
      "Missing atoms [('E', 12), ('F', 21), ('E', 21), ('C', 9), ('D', 16)]\n",
      "Parsing pdb_file/X-RAY/5YTX.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5YTS.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5YTT.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5YTV.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/X-RAY/5YKI.pdb\n",
      "Chain list: C\n",
      "Parsing pdb_file/X-RAY/5DDP.pdb\n",
      "Chain list: A B\n",
      "Parsing pdb_file/X-RAY/5DDO.pdb\n",
      "Chain list: A B\n",
      "Parsing pdb_file/X-RAY/5DDR.pdb\n",
      "Chain list: A B\n",
      "Parsing pdb_file/X-RAY/5DDQ.pdb\n",
      "Chain list: A B\n",
      "Parsing pdb_file/X-RAY/5DE5.pdb\n",
      "Chain list: A C\n",
      "Parsing pdb_file/X-RAY/5DET.pdb\n",
      "Chain list: Q P\n",
      "Parsing pdb_file/X-RAY/5DEA.pdb\n",
      "Chain list: A C\n",
      "Parsing pdb_file/X-RAY/5DE8.pdb\n",
      "Chain list: A C\n",
      "Parsing pdb_file/X-RAY/6C6K.pdb\n",
      "Chain list: E F\n",
      "Parsing pdb_file/X-RAY/6D12.pdb\n",
      "Chain list: C\n",
      "Parsing pdb_file/X-RAY/5W0G.pdb\n",
      "Parsing pdb_file/X-RAY/5W0H.pdb\n",
      "Parsing pdb_file/X-RAY/6N4O.pdb\n",
      "Chain list: C D\n",
      "Missing atoms [('D', 6)]\n",
      "Parsing pdb_file/X-RAY/6N6I.pdb\n",
      "Chain list: D C\n",
      "Parsing pdb_file/X-RAY/6N6K.pdb\n",
      "Chain list: D C\n",
      "Parsing pdb_file/X-RAY/6N6J.pdb\n",
      "Chain list: D C\n",
      "Parsing pdb_file/NMR/2AD9.pdb\n",
      "Chain list: B\n",
      "Parsing pdb_file/NMR/2ADC.pdb\n",
      "Chain list: B C\n",
      "Parsing pdb_file/X-RAY/2HYI.pdb\n",
      "Chain list: F L\n",
      "Parsing pdb_file/X-RAY/3EX7.pdb\n",
      "Chain list: F J\n"
     ]
    }
   ],
   "source": [
    "for pdb_id in pdb_idlist:\n",
    "    if not os.path.exists(\"pdb_file\"):\n",
    "        os.mkdir(\"pdb_file\")\n",
    "    method = get_method(pdb_id)\n",
    "    if not os.path.exists(\"pdb_file/\"+method):\n",
    "        os.mkdir(\"pdb_file/\"+method)\n",
    "    if not os.path.exists(\"pdb_file/\"+method+\"/\"+pdb_id+\".pdb\"):\n",
    "        with open(\"pdb_file/\"+method+\"/\"+pdb_id+\".pdb\", \"w\") as f:\n",
    "            f.write(get_pdb_file(pdb_id))\n",
    "    if not os.path.exists(\"results\"):\n",
    "        os.mkdir(\"results\")\n",
    "        \n",
    "    rna_list = build_rna_list(pdbfile(\"pdb_file/\"+method+\"/\"+pdb_id+\".pdb\"))\n",
    "\n",
    "    if not os.path.exists(\"results/\"+pdb_id):\n",
    "        os.mkdir(\"results/\"+pdb_id)\n",
    "    with open(\"results/\"+pdb_id+\"/description.txt\", \"w\", encoding=\"utf-8\") as f:\n",
    "        for rna in rna_list:\n",
    "            print(pdb_id, rna, rna.sequence, file=f)\n",
    "            calculate_internal(rna, \"results/\"+pdb_id+\"/\")\n",
    "    if len(rna_list) > 1:\n",
    "        for rna1, rna2 in combinations(rna_list, 2):\n",
    "            calculate(rna1, rna2, \"results/\"+pdb_id+\"/\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
